package org.dromara.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.WbCompanyBlacklist;
import org.dromara.system.domain.bo.WbCompanyBlacklistBo;
import org.dromara.system.domain.bo.WbCompanyInfoBo;
import org.dromara.system.domain.dto.WbCompanyBlacklistCountProductDTO;
import org.dromara.system.domain.dto.WbCompanyBlacklistCountProvinceDTO;
import org.dromara.system.domain.vo.WbCompanyBlacklistVo;
import org.dromara.system.domain.vo.WbCompanyInfoVo;
import org.dromara.system.domain.vo.WbDemandListVo;
import org.dromara.system.listener.WbCompanyBlacklistImportListener;
import org.dromara.system.listener.WbDemandListImportListener;
import org.dromara.system.service.IWbCompanyBlacklistService;
import org.dromara.system.service.IWbCompanyInfoService;
import org.dromara.system.service.IWbProvinceService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 企业黑名单
 *
 * @author Lion Li
 * @date 2024-03-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/companyBlacklist")
public class WbCompanyBlacklistController extends BaseController {

    private final IWbCompanyBlacklistService wbCompanyBlacklistService;
    private final IWbCompanyInfoService wbCompanyInfoService;

    /**
     * 查询企业黑名单列表
     */
    @SaCheckPermission("system:companyBlacklist:list")
    @GetMapping("/list")
    public TableDataInfo<WbCompanyBlacklistVo> list(WbCompanyBlacklistBo bo, PageQuery pageQuery) {
        return wbCompanyBlacklistService.queryPageListZdy(bo, pageQuery);
    }

    /**
     * 导出企业黑名单列表
     */
    @SaCheckPermission("system:companyBlacklist:export")
    @Log(title = "企业黑名单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WbCompanyBlacklistBo bo, HttpServletResponse response) {
        List<WbCompanyBlacklistVo> list = wbCompanyBlacklistService.queryList(bo);
        ExcelUtil.exportExcel(list, "企业黑名单", WbCompanyBlacklistVo.class, response);
    }

    /**
     * 导出企业黑名单列表模板
     */
    @SaCheckPermission("system:companyBlacklist:export")
    @Log(title = "企业黑名单", businessType = BusinessType.EXPORT)
    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<WbCompanyBlacklistVo> list = new ArrayList<>();
        WbCompanyBlacklistVo wbCompanyBlacklistVo = new WbCompanyBlacklistVo();
        wbCompanyBlacklistVo.setRemark("河北公司在2023年10月份抽检中，发现1次3级质量问题。");
        wbCompanyBlacklistVo.setDisposeWay("10kV及以下低压开关柜");


        list.add(wbCompanyBlacklistVo);
        ExcelUtil.exportExcel(list, "企业黑名单", WbCompanyBlacklistVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     */
    @Log(title = "黑名单", businessType = BusinessType.IMPORT)
    @SaCheckPermission("system:companyBlacklist:add")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<WbCompanyBlacklistVo> result = ExcelUtil.importExcel(file.getInputStream(), WbCompanyBlacklistVo.class,new WbCompanyBlacklistImportListener());
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取企业黑名单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:companyBlacklist:query")
    @GetMapping("/{id}")
    public R<WbCompanyBlacklistVo> getInfo(@NotNull(message = "主键不能为空")
                                           @PathVariable Long id) {
        return R.ok(wbCompanyBlacklistService.queryById(id));
    }

    /**
     * 新增企业黑名单
     */
    @SaCheckPermission("system:companyBlacklist:add")
    @Log(title = "企业黑名单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WbCompanyBlacklistBo bo) {
        wbCompanyInfoService.handleName(bo.getCompanyName());
        return toAjax(wbCompanyBlacklistService.insertByBo(bo));
    }

    /**
     * 修改企业黑名单
     */
    @SaCheckPermission("system:companyBlacklist:edit")
    @Log(title = "企业黑名单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WbCompanyBlacklistBo bo) {
        wbCompanyInfoService.handleName(bo.getCompanyName());
        return toAjax(wbCompanyBlacklistService.updateByBo(bo));
    }

    /**
     * 删除企业黑名单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:companyBlacklist:remove")
    @Log(title = "企业黑名单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wbCompanyBlacklistService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 批量发布项目
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:companyBlacklist:edit")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PutMapping("/release/{ids}")
    public R<Void> release(@NotEmpty(message = "主键不能为空")
                           @PathVariable Long[] ids) {
        for (Long id : ids) {
            WbCompanyBlacklistVo wbCompanyBlacklistVo = wbCompanyBlacklistService.queryById(id);
            if("-1".equals(wbCompanyBlacklistVo.getStatus())){
                wbCompanyBlacklistVo.setStatus("0");
                wbCompanyBlacklistService.updateByBo(BeanUtil.toBean(wbCompanyBlacklistVo, WbCompanyBlacklistBo.class));
            }
        }
        return R.ok();
    }

}
